using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using YDExtendTool.Scripts;
using Object = UnityEngine.Object;

namespace DDA
{
    [CreateAssetMenu(fileName = "DDATree",menuName = "TreeData/DDA")]
    public class DynamicDifficultAdjustTree : TreeNodeDataBase
    {
        public string rootPath = "DDA";
        protected override string ResourcePath()
        {
            return $"{rootPath}/{name}";
        }
        



        [Button("重置树")]
        private void Reset()
        {
            if (RootNode is DDATreeNodeBase ddaNode)
            {
                ddaNode.Reset();
            }
        }
    }
    public enum DDATreeNodeState
    {
        Disable,
        Enable,
    }

    public abstract class DDATreeNodeBase : TreeNodeMultipleNextBase
    {
        // 进入当前节点时触发
        [ShowInInspector,LabelText("子节点最大数量")] public abstract int NextNodeMaxNum { get; }
        private List<DDATreeNodeBase> ddaChildNodes = null;

        [ShowInInspector, LabelText("状态")] public DDATreeNodeState state;
        public new List<DDATreeNodeBase> ChildNodes
        {
            get
            {
                if (ddaChildNodes == null)
                {
                    // 清除空节点
                    for(int i=base.ChildNodes.Count-1;i>=0;i--)
                    {
                        if (base.ChildNodes[i] == null)
                        {
                            base.ChildNodes.RemoveAt(i);
                        }
                    }
                    ddaChildNodes = base.ChildNodes.ConvertAll((x) => x as DDATreeNodeBase);
                }

                return ddaChildNodes;
            }
        }

        public virtual bool CanConnect(TreeNodeBase treeNodeBase)
        {
            return base.ChildNodes.Count < NextNodeMaxNum;
        }

        public virtual bool Invoke()
        {
            state = DDATreeNodeState.Enable;
            return true;
        }

        public virtual void Reset()
        {
            state = DDATreeNodeState.Disable;
            foreach (var _ in ChildNodes)
            {
                if (_ is { } ddaTreeNodeBase)
                {
                    ddaTreeNodeBase.Reset();
                }
            }
        }

        public override void OnConnected(TreeNodeBase node)
        {
            base.OnConnected(node);
        }

        public override Type TreeNodeBaseType => typeof(DDATreeNodeBase);
    }

    public class DDALog : DDATreeNodeBase
    {
        [ShowInInspector] public string text = "";

        public override int NextNodeMaxNum { get; } = 0;

        public override bool Invoke()
        {
            Debug.Log(text);
            return true;
        }

        public override void Reset()
        {
            
        }
    }

    /// <summary>
    /// 过渡节点
    /// </summary>
    public class DDATransition : DDATreeNodeBase
    {
        public override int NextNodeMaxNum { get; } = 10;
        public override bool Invoke()
        {
            for (int i = 0; i < ChildNodes.Count; i++)
            {
                ChildNodes[i].Invoke();
            }

            return true;
        }
    }

}


